/*
 * @Description: app原生插件工具类.统一出口调用原生插件。以便调试环境能模拟插件运行
 * @Author: tao.xie
 * @Date: 2019-03-26 11:06:00
 * @Last Modified by: ziwei.ma
 * @Last Modified time: 2019-05-10 18:46:02
 */
import { Toast as MToast } from 'mint-ui'
import 'mint-ui/lib/toast/style.css'

let NativeUtils = {}
NativeUtils.install = function(Vue, options) {
  // toast提示。生产环境调用插件，调试环境调用alert
  Vue.prototype.$toastMsg = function(msg) {
    if (window.prodInApp) {
      this.$Toast.then(Toast => {
        Toast.showShortBottom(msg)
      })
    } else {
      // alert('toastMsg:' + msg)
      MToast({
        message: msg,
        position: 'bottom',
        duration: 3000
      })
    }
  }

  // 支付宝支付结果。生产环境调用插件，调试环境执行回调函数
  Vue.prototype.$doPayment = function(
    payName,
    payInfo,
    successCallback,
    errorCallback
  ) {
    if (window.prodInApp) {
      this.$ThirdParty.then(ThirdParty => {
        ThirdParty.doPayment(payName, payInfo, successCallback, errorCallback)
      })
    } else {
      console.log('payName--->', payName)
      console.log('payInfo--->', payInfo)

      if (typeof successCallback === 'function') {
        successCallback()
      } else {
        console.log('successCallback--->', successCallback)
      }

      if (typeof errorCallback === 'function') {
        errorCallback()
      } else {
        console.log('errorCallback--->', errorCallback)
      }
    }
  }

  // 支付方式获取，调用插件执行回调函数
  Vue.prototype.$getPaymentSupportList = function() {
    return new Promise((resolve, reject) => {
      if (window.prodInApp) {
        this.$ThirdParty.then(ThirdParty => {
          ThirdParty.getPaymentSupportList(resolve, reject)
        })
      } else {
        console.log('emmmmmmmmm')
        resolve('["WXPAY", "ALIPAY"]')
        reject(new Error(''))
      }
    })
  }
}

export default NativeUtils
